package com.magicsoft.share.presenter.article;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.article.MyArticleDetail;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.article.IAddArticleView;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/12/7/007.
 */

public class AddArticlePresenter extends BasePresenter<IAddArticleView> {
    public void articleEdit(Long id,
            String title, String cover, String coverRiginal,
            String content, Integer isAllowComment, Integer isOpen, Integer articleType,
            Integer special, Integer status) {
        RetrofitUtil.getApi(APIService.class)
                .articleEdit(SPUtils.getInstance().getString(SPKey.TOKEN), SPUtils.getInstance().getLong(SPKey.ID), id,title, cover, coverRiginal, content, isAllowComment, isOpen, articleType, special, status)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().articleEditSuccess();
                    }


                });
    }


    public void myArticleDetail(long id){
        RetrofitUtil.getApi(APIService.class)
                .myArticleDetail(SPUtils.getInstance().getString(SPKey.TOKEN),id)
                .compose(new NetWorkTransformer<MyArticleDetail>())
                .compose(new LoadingTransformer<MyArticleDetail>(mContext))
                .subscribe(new SimpleObserver<MyArticleDetail>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull MyArticleDetail myArticleDetail) {
                        getView().myArticleDetailSuccess(myArticleDetail);
                    }
                });

    }
    public void deleteArticle(long articleId) {
        RetrofitUtil.getApi(APIService.class)
                .deleteArticle(SPUtils.getInstance().getString(SPKey.TOKEN), articleId)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull Object o) {

                        getView().deleteArticleSuccess();
                    }
                });
    }
}
